{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "97c4c726",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/10/23/week 09\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4029d160",
   "metadata": {},
   "source": [
    "# 集合（大括号）\n",
    "\n",
    "> 1.集合不允许重复\n",
    "\n",
    "> 2.集合以大括号开始和结束，使用逗号作为分隔符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d245da08",
   "metadata": {},
   "source": [
    "### 一.set 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02efbb6a",
   "metadata": {},
   "source": [
    "* 从序列创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ff505da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 未使用set函数前\n",
    "vowels = {'a','e','e','i','o','u','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85c0eb2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用set函数后\n",
    "vowels = set('aeeiouu')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70a3176c",
   "metadata": {},
   "source": [
    "### 二.union合并集合（并集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4c26c5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7ec2d60",
   "metadata": {},
   "source": [
    "* 将集合转化为有序列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fd0c0c3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list = sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1035b8c6",
   "metadata": {},
   "source": [
    "### 三.difference指出共有元素（差集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fb1b9a1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ee3c761b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'l'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = set(word).difference(vowels)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad51f5d3",
   "metadata": {},
   "source": [
    "### 四.intersection报告共同对象（交集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "df63eeca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cdd99f4",
   "metadata": {},
   "source": [
    "### 五.P131测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9d89ff84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:wu,hui,chun\n",
      "u\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowels in found:\n",
    "    print(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efecdc49",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
